<template>
    <div class="login-wrap">
        <div class="ms-login">
            <div class="ms-title"></div>
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="ms-content">
                <el-form-item prop="username">
                    <el-input v-model="ruleForm.username" placeholder="username">
                        <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" placeholder="password" v-model="ruleForm.password" @keyup.enter.native="submitForm">
                        <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                    </el-input>
                </el-form-item>
                <div class="login-btn">
                    <el-button class="but" @click="submitForm">登录</el-button>
                </div>
                <p class="login-tips"></p>
            </el-form>
        </div>
    </div>
</template>

<script>
import axios from 'axios';
import { mapMutations } from "vuex";
import Cookies from "js-cookie";
    export default {
        data: function(){
            return {
                ruleForm: {
                    username: '',
                    password: ''
                },
                rules: {
                    username: [
                        { required: true, message: '请输入用户名', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' }
                    ]
                },
                
            }
        },
        //上传
        methods: {
            ...mapMutations(["changeLogin"]),
            submitForm() {
                let _this = this;
               axios({
                    url: "/api/login/login",
                    method: "post",
                    params: {
                        username: this.ruleForm.username,
                        password: this.ruleForm.password,
                    }
                }).then(res => {
                      if(res.data.code==250){
                          this.$message({
                            message: '账号错误',
                            type: 'error'
                        });
                      }else if(res.data.code==251){
                          this.$message({
                            message: '密码错误',
                            type: 'error'
                        });
                      }else{

                        this.$message({
                                message: '登录成功',
                                type: 'success'
                        });


                       localStorage.setItem('staff',JSON.stringify(res.data.data));
                      
                        
                       this.$router.push({ path: '/productlist' });

                        console.log(res);
                        console.log(res.data.data);
                        _this.userToken =  res.data.data.token;
                        // 将用户token保存到vuex中
                        _this.changeLogin({ Authorization: _this.userToken });
                        Cookies.set("Token", res.data.data.token); //登录成功后将token存储在cookie之中
                        _this.$router.push("/Dashboard");




                }

                })
                },
        }
    }
</script>

<style scoped>
    .login-wrap{
        position: relative;
        width:100%;
        height:100%;
        background-image: url(../../assets/img/mei.jpg);
        background-size: 100%;
    }
    .ms-title{
        width:100%;
        line-height: 50px;
        text-align: center;
        font-size:20px;
        color: #000;
        border-bottom: 1px solid rgb(94, 9, 9);
       
    }
    /*  按钮 */
   .but{
        background-color: #648171;
   }
    .ms-login{
        position: absolute;
       left: 15%;
        top:40%;
        width:350px;
        margin:-190px 0 0 -175px;
        border-radius: 5px;
        background: transparent;
        overflow: hidden;
    }
    .ms-content{
        padding: 30px 30px;
        
    }
    .login-btn{
        text-align: center;
    }
    .login-btn button{
        width:100%;
        height:36px;
        margin-bottom: 10px;
    }
    .login-tips{
        font-size:12px;
        /* line-heigh:#000; */
    }
</style>